package com.njit.mailm.core.controller;

import com.njit.mailm.commmon.constant.MailConstant;
import com.njit.mailm.core.bean.User;
import com.njit.mailm.core.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.Projection;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/8.
 */
@RequestMapping(value = "/mail")
@Controller
public class UploadController {
    @Autowired
    private UploadService uploadService;

    @RequestMapping(value = "uploadAttachment",method = RequestMethod.POST)
    public String uploadAttachment(@RequestParam("file") MultipartFile file, HttpSession session,String composeId){

        Map<String,Object> result = new HashMap<String, Object>();
        User user = (User) session.getAttribute(MailConstant.CURRENTUSER);
        String basePath = session.getServletContext().getRealPath("/") + "WEB-INF/upload";
        System.out.println("basePath"+basePath);
        System.out.println("composeId:"+composeId);
        boolean isSuccess = uploadService.uploadAttchement(user, file, basePath,composeId);
        result.put("success",isSuccess);
        return "success";
    }
}
